package gamegame;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class BathroomLock extends JFrame{
	JSpinner one, two, three, four;
	SpinnerListModel spinOne,spinTwo, spinThree, spinFour;
	JButton tryLock;
	JLabel wrong;
	String[] list;
	public BathroomLock(final String code){//this again
		super("Bathroom Lock");
		setLayout(new GridBagLayout());
		GridBagConstraints c=new GridBagConstraints();
		list=new String[]{"0","1","2","3","4","5","6","7","8","9"};
		c.insets=new Insets(20,20,20,20);
		c.anchor=GridBagConstraints.CENTER;
		spinOne=new SpinnerListModel(list);
		c.gridx=0;
		c.gridy=0;
		c.ipadx=100;
		c.ipady=50;
		one=new JSpinner(spinOne);
		one.setValue("5");
		one.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				if(two.getNextValue()!=null){
					two.setValue(two.getNextValue());
				}
				else{
					two.setValue("0");
				}
			}
			
		});
		add(one, c);
		spinTwo=new SpinnerListModel(list);
		c.gridx=1;
		c.gridy=0;
		two=new JSpinner(spinTwo);
		two.setValue("2");
		two.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				four.setValue("5");
			}
			
		});
		add(two, c);
		spinThree=new SpinnerListModel(list);
		c.gridx=2;
		c.gridy=0;
		three=new JSpinner(spinThree);
		three.setValue("7");
		three.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				if(one.getNextValue()!=null){
					one.setValue(two.getNextValue());
				}
				else{
					two.setValue("6");
				}
			}
			
		});
		add(three, c);
		spinFour=new SpinnerListModel(list);
		c.gridx=3;
		c.gridy=0;
		four=new JSpinner(spinFour);
		four.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				if(!three.getValue().equals("0")){
					three.setValue(three.getPreviousValue());
				}
			}
			
		});
		add(four, c);
		wrong=new JLabel("The lock doesn't budge.");
		c.insets=new Insets(20,125,20,20);
		c.gridx=1;
		c.gridy=1;
		c.gridwidth=2;
		wrong.setVisible(false);
		add(wrong, c);
		tryLock=new JButton("Try Lock");
		c.insets=new Insets(20,20,20,20);
		c.gridx=1;
		c.gridy=2;
		tryLock.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String test=(String)one.getValue()+(String)two.getValue()+(String)three.getValue()+(String)four.getValue();
				if(test.equals(code)){
					dispose();
				}
				else{
					wrong.setVisible(true);
				}
			}
		});
		add(tryLock, c);
		
	}
}
